﻿using System.Collections.Generic;
using System.Linq;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;
using Harvest.Api.Types;

namespace Harvest.Api.ExtensionMethods
{

    public static class UserExtensions
    {

        public static User ToUser(this UserItem entity)
        {
            var result = new User
            {
                DefaultHourlyRate = entity.DefaultHourlyRate.HasValue ? entity.DefaultHourlyRate.Value : (double?)null,
                Department = entity.Department,
                Email = entity.Email,
                FirstName = entity.FirstName,
                HasAccessToAllFutureProjects = entity.HasAccessToAllFutureProjects,
                Id = entity.Id.HasValue ? entity.Id.Value : (int?)null,
                IsActive = entity.IsActive,
                IsAdmin = entity.IsAdmin,
                IsContractor = entity.IsContractor,
                LastName = entity.LastName,
                Telephone = entity.Telephone,
                Timezone = entity.Timezone
            };
            return result;
        }


        public static UserItem ToUserItem(this User entity)
        {
            var id = new NullableInt();
            if (entity.Id.HasValue) id = entity.Id.Value;

            var defaultHourlyRate = new NullableDouble();
            if (entity.DefaultHourlyRate.HasValue) defaultHourlyRate = entity.DefaultHourlyRate.Value;

            var result = new UserItem
            {
                DefaultHourlyRate = defaultHourlyRate,
                Department = entity.Department,
                Email = entity.Email,
                FirstName = entity.FirstName,
                HasAccessToAllFutureProjects = entity.HasAccessToAllFutureProjects,
                Id = id,
                IsActive = entity.IsActive,
                IsAdmin = entity.IsAdmin,
                IsContractor = entity.IsContractor,
                LastName = entity.LastName,
                Telephone = entity.Telephone,
                Timezone = entity.Timezone
            };
            return result;
        }


        public static List<User> ToUsers(this IEnumerable<UserItem> entity)
        {
            var result = entity.Select(x => x.ToUser()).ToList();
            return result;
        }


        public static List<UserItem> ToUsersItem(this IEnumerable<User> entity)
        {
            var result = entity.Select(x => x.ToUserItem()).ToList();
            return result;
        }
    }

}
